/*******************************************************************************
** CopyRight (c) 2005-2008 SKY-MOBI Ltd. All rights reserved.
** Filename:  dl_win.c
** Creator:   yuzhifeng
** Version:   0.0
** Date:      
** Description:
 *******************************************************************************/

#ifdef HAS_SEARCH
#include "commdef.h"
#else
#include "commdef.h"
#include "mpsFuncPool.h"

#include "appmgr_i.h"
#include "appmgr.h"
#include "sgl_window.h"
#include "smp.h"
#include "smp_editEx.h"
#include "smp_combobox.h"
#include "smp_button.h"
#include "smp_toolbar.h"
#include "string.h"
#include "res.h"
#ifndef SDK_NO_PLUGIN
#include "sgl_string.h"
#include "appmgr_string.h"
#else
#include "cookie_string.h"
#endif
#include "appmgr_guimain.h"

#define HAS_BOTTOM_AD 0

#define WIN_MARGIN   4
#define KEYWORD_LEN 24

enum {
	SRCH_EDITOR,
	SRCH_COMBO,
	SRCH_BUTTON,
	SRCH_HOTKEY1
};

typedef struct {
	char keyword[KEYWORD_LEN];
	
} SEARCH_DATA;

LRESULT Search_WinProc(HWND hWnd, UINT Msg, WPARAM wParam, LPARAM lParam)
{

	HWND hControl;
	SEARCH_DATA* pData = _GET_WINDATA(hWnd, SEARCH_DATA*);

	switch(Msg)
	{

	case WM_CREATE:
	{
		int32 x, y, w, h; 
		HFONT font = SGL_GetSystemFont();
		
		pData = SGL_MALLOC(sizeof(SEARCH_DATA));
		memset(pData, 0, sizeof(SEARCH_DATA));
		_SET_WINDATA(hWnd, pData);
		GAL_textWidthHeight((char *)SGL_LoadString(STR_SEARCH_SCOPE), 1, font, &w, &h);
		
		x = w+2*WIN_MARGIN;
		y = SMP_TITLE_HEIGHT + WIN_MARGIN;
		hControl = SMP_CreateWindow(SMP_EDITEX_WNDPROC, 
			2*WIN_MARGIN, y, _WIDTH(hWnd)-3*WIN_MARGIN, SMP_TITLE_HEIGHT,
			SRCH_EDITOR, WS_TABSTOP | ES_ALPHA | SMP_EDITEXS_SHOWTITLE, 0);
		SMP_EditEx_SetInfo(hControl, SGL_LoadString(STR_KEYWORD), pData->keyword, KEYWORD_LEN+2);
		SGL_AddChildWindow(hWnd, hControl);
		
		y += h+2 +WIN_MARGIN;
		hControl = SMP_CreateWindow(SMP_COMBOBOX_WNDPROC, 
			x, y, _WIDTH(hWnd)-3*WIN_MARGIN-w, h+2,
			SRCH_COMBO, WS_TABSTOP, 0);
		SMP_ComboBox_SetSize(hControl, 2);
		SMP_ComboBox_SetItem(hControl, 0, SGL_LoadString(STR_ONLINE_SEARCH), 0);
		SMP_ComboBox_SetItem(hControl, 1, SGL_LoadString(STR_LOCAL_SEARCH), 0);
		SGL_AddChildWindow(hWnd, hControl);

		y += h+2 +WIN_MARGIN;
		GAL_textWidthHeight((char *)SGL_LoadString(STR_SEARCH), 1, font, &w, &h);
		x = DIV(_WIDTH(hWnd)-(w+2*WIN_MARGIN+2), 2);
		hControl = SMP_CreateWindow(SMP_BUTTON_WNDPROC, 
			x, y, (w+2*WIN_MARGIN+2), h+4,
			SRCH_BUTTON, WS_TABSTOP | WS_BORDER, 0);
		SMP_Button_SetData(hControl, NULL, SGL_LoadString(STR_SEARCH));
		SGL_AddChildWindow(hWnd, hControl);
			
		SMP_CreateToolbar(hWnd, STR_OK, STR_BACK);
 
		return 0;
	}
	case WM_DESTROY:
	{
		if(pData)
			free(pData);
		break;
	}
	case WM_INITFOCUS:
	{
		hControl = SGL_FindChildWindow(hWnd, SRCH_EDITOR);
		SGL_SetFocusWindow(hWnd, hControl);
		return 0;
	}
	case WM_PAINT:
	{
		int32 x = 0, y = 0, w, h; 
		HFONT font = SGL_GetSystemFont();
		bmpSt Icon;

		SGL_WindowToScreen(hWnd, &x, &y);
		y += SMP_TITLE_HEIGHT + WIN_MARGIN + 1;
		GAL_textWidthHeight((char *)SGL_LoadString(STR_SEARCH_SCOPE), 1, font, &w, &h);
		y += SMP_ITEM_HEIGHT;
		GAL_drawText((uint8 *)SGL_LoadString(STR_SEARCH_SCOPE), WIN_MARGIN+x, y, 0x00ffffff, 1, font);

		y += _WIDTH(hWnd) - 3*SMP_ITEM_HEIGHT;
		GAL_drawText((uint8 *)SGL_LoadString(STR_HOT_DOWNLOAD), WIN_MARGIN+x, y, 0x00ffffff, 1, font);

		Icon.pData = SGL_LoadBitmap(BMP_SUB_TITLE, &Icon.width, &Icon.height);
		SMP_DrawTopWinHeader((DWORD)&Icon, (PCWSTR)SGL_LoadString(STR_SEARCH));		
		break;
	}
	case WM_KEYDOWN:
	case WM_KEYDOWNREPEAT:
	{
		switch(wParam)
		{
		case MR_KEY_DOWN:
			SGL_FocusNext(hWnd, 1);
			break;
		case MR_KEY_UP:
			SGL_FocusNext(hWnd, 0);
			break;
		}
		return 0;
	}
	case WM_KEYUP:
	{
		switch(wParam)
		{
		case MR_KEY_SOFTRIGHT:
			ALU_HideTopWindow(TOPWIN_SEARCH, TRUE, TRUE);
			break;
		}
		return 0;
	}

	case WM_COMMAND:
	{
		WID id = LOWORD(wParam);
		//WORD code = HIWORD(wParam);

		if(id == SRCH_BUTTON)
		{}
		return 0;
	}
	}
	return 0;
   }

API int32 AppMgr_ShowSearchWin(void)
{
	ALU_ShowTopWindow(TOPWIN_SEARCH, NULL, 0);

	return MR_SUCCESS;
}
#endif

